/**
 * 用户登录
 * 
 * @author xuhongbao
 */

Ext.namespace('ESBuilder.mgmt.login')
ESBuilder.mgmt.login.TRANS_LOGIN = 'Login.do';
// 产生验证码交易
ESBuilder.mgmt.login.TRANS_GENTOKENIMG = 'GenTokenImg.do';

ESBuilder.mgmt.login.Window = function(config) {
	Ext.apply(this, config);
	var win = this;
	var roleCombox = new Ext.form.ComboBox({ // select
		fieldLabel : '角色',
		editable : false,
		triggerAction : 'all',
		allowBlank : false,
		valueField : 'roleId',
		displayField : 'name',
		width : 115,
		mode : 'local',
		store : ESBuilder.prm.constants.RoleStore,
		emptyText : '请选择角色...',
		hiddenName : 'roleId'
	});
	var formPanel = new Ext.form.FormPanel({
				bodyStyle : 'padding:70px 0 40px 0',
				defaultType : 'textfield',
				labelAlign : 'right',
				labelWidth : 180,
				labelPad : 0,
				keys : [{
							key : [10, 13],
							fn : this.submitButtonAction,
							scope : this
						}],
				frame : true,
				// minButtonWidth : 60,
				url : ESBuilder.mgmt.login.TRANS_LOGIN,
				defaults : {
					allowBlank : false,
					msgTarget : 'side', // 验证信息显示右边
					width : 158
				},
				items : [new Ext.form.Hidden({
									id : '_viewReferer',
									name : '_viewReferer',
									value : 'Main'

								}), roleCombox,

						{
							name : 'LoginId',
							fieldLabel : '登录名',
							blankText : '登录名不能为空'
						}, {
							name : 'Password',
							fieldLabel : '登录密码',
							blankText : '登录密码不能为空',
							inputType : 'password'
						}, {
							xtype : 'textfield',
							name : '_vTokenName',
							id : 'validCode',
							fieldLabel : '验证码',
							blankText : '验证码不能为空',
							width : 60,
							allowBlank : false
						}]
			});

	this.formPanel = formPanel;

	this.submitButton = new Ext.Button({
				id : 'loginButton',
				text : '登录',
				disabled : false,
				minWidth : 75,
				scope : this,
				handler : this.submitAction
			});
	this.resetButton = new Ext.Button({
				text : '重置',
				minWidth : 75,
				disabled : false,
				scope : this,
				handler : this.resetAction
			});
	/*
	 * var f = this.submitButtonAction; var t = this; var map = new
	 * Ext.KeyMap("loginButton", { key : 13, // or Ext.EventObject.ENTER fn : f,
	 * scope : t });
	 */
	ESBuilder.mgmt.login.Window.superclass.constructor.call(this, {
				title : '请登录',
				buttonAlign : 'center',
				width : 500,
				height : 300,
				collapsible : true,
				layout : 'fit',
				border : false,
				frame : true,
				closable : false,
				modal : true,
				closeAction : 'close',
				items : [this.formPanel],
				buttons : [this.submitButton, this.resetButton]
			});
}
/* 主窗口 */
Ext.extend(ESBuilder.mgmt.login.Window, Ext.Window, {
			/* 提交 */
			submitAction : function() {

				var btn = this.submitBtn;
				var wind = this;
				var frm = this.formPanel.getForm();

				var bt = this;

				if (frm.isValid()) {
					this.disable();
					frm.submit({
								waitTitle : '登录提示',
								waitMsg : '正在为您登录,请稍候.....',
								success : function(form, action) {
									wind.close();
									displayMain();
									displayTop();

								},
								failure : function(form, action) {

									Ext.Msg.show({
												title : '错误提示',
												msg : action.result.msg,
												buttons : Ext.Msg.OK,
												fn : function() {

													bt.enable();
													// 刷新验证码
													ESBuilder.mgmt.login
															.changeValidCode();
												},
												icon : Ext.Msg.ERROR
											});
								}
							})
				}

			},
			resetAction : function() {
				var frm = this.formPanel.getForm();
				frm.reset();

			}
		});

// 模拟登录window
ESBuilder.mgmt.login.WindowTest = function(config) {
	Ext.apply(this, config);
	var win = this;
	var roleCombox = new Ext.form.ComboBox({ // select
		fieldLabel : '角色',
		editable : false,
		triggerAction : 'all',
		allowBlank : false,
		valueField : 'roleId',
		displayField : 'name',
		width : 115,
		mode : 'local',
		store : ESBuilder.prm.constants.RoleStoreTest,
		emptyText : '请选择角色...',
		hiddenName : 'roleId'
	});
	var formPanel = new Ext.form.FormPanel({
				bodyStyle : 'padding:70px 0 40px 0',
				defaultType : 'textfield',
				labelAlign : 'right',
				labelWidth : 180,
				labelPad : 0,
				keys : [{
							key : [10, 13],
							fn : this.submitButtonAction,
							scope : this
						}],
				frame : true,
				// minButtonWidth : 60,
				url : '/snackMgmt/index.htm',
				defaults : {
					allowBlank : false,
					msgTarget : 'side', // 验证信息显示右边
					width : 158
				},
				items : [new Ext.form.Hidden({
									id : '_viewReferer',
									name : '_viewReferer',
									value : 'Main'

								}), roleCombox,

						{
							name : 'LoginId',
							fieldLabel : '登录名',
							blankText : '登录名不能为空'
						}, {
							name : 'Password',
							fieldLabel : '登录密码',
							blankText : '登录密码不能为空',
							inputType : 'password'
						}]
			});

	this.formPanel = formPanel;

	this.submitButton = new Ext.Button({
				id : 'loginButton',
				text : '登录',
				disabled : false,
				minWidth : 75,
				scope : this,
				handler : this.submitAction
			});
	this.resetButton = new Ext.Button({
				text : '重置',
				minWidth : 75,
				disabled : false,
				scope : this,
				handler : this.resetAction
			});
	/*
	 * var f = this.submitButtonAction; var t = this; var map = new
	 * Ext.KeyMap("loginButton", { key : 13, // or Ext.EventObject.ENTER fn : f,
	 * scope : t });
	 */
	ESBuilder.mgmt.login.WindowTest.superclass.constructor.call(this, {
				title : '请登录',
				buttonAlign : 'center',
				width : 500,
				height : 300,
				collapsible : true,
				layout : 'fit',
				border : false,
				frame : true,
				closable : false,
				modal : true,
				closeAction : 'close',
				items : [this.formPanel],
				buttons : [this.submitButton, this.resetButton]
			});
}
Ext.extend(ESBuilder.mgmt.login.WindowTest, Ext.Window, {
			/* 提交 */
			submitAction : function() {

				var btn = this.submitBtn;
				var wind = this;
				var frm = this.formPanel.getForm();

				var bt = this;

				if (frm.isValid()) {
//					this.disable();
					frm.submit({
								waitTitle : '登录提示',
								waitMsg : '正在为您登录,请稍候.....',
								success : function(form, action) {
									displayMain();
									displayTop();
									wind.close();

								},
								failure : function(form, action) {

									Ext.Msg.show({
												title : '错误提示',
												msg : action.result.msg,
												buttons : Ext.Msg.OK,
												fn : function() {

													bt.enable();;
												},
												icon : Ext.Msg.ERROR
											});
								}
							})
				}

			},
			resetAction : function() {
				var frm = this.formPanel.getForm();
				frm.reset();

			}
		});

ESBuilder.mgmt.login.displayLogin = function() {
	// if (Ext.getDom('userIn').value=='true') {增加登录流程
	// var win = new ESBuilder.mgmt.login.Window({});
	// win.show();
	// ESBuilder.mgmt.login.genToken();
	// } else {
	// displayMain();
	// displayTop();
	// }

	// 模拟登录界面
	var win = new ESBuilder.mgmt.login.WindowTest({});
	win.show();
}

Ext.onReady(function() {
			Ext.get('loading').remove();
			Ext.getDom('top').style.visibility = 'visible';
			ESBuilder.mgmt.login.displayLogin();
		});
